#ifndef _CBITDAEMON_H_
#define _CBITDAEMON_H_

#include <buola/bbox/bservice.h>
#include <buola/app/ccmdline.h>
#include <buola/io/curi.h>
#include <set>

namespace buola { namespace bitdaemon {

class CTorrentManager;
class CDownload;

class CBitDaemon : public bbox::BService<CBitDaemon>
{
public:
    CBitDaemon();
    ~CBitDaemon();

    CTorrentManager &TorrentManager()    {   return *mTorrentManager;     }

    void AddDownload(CDownload *pDownload);
    void RemoveDownload(CDownload *pDownload);

private:
    void OpenTorrent(const io::CURI &pURI,const io::CURI &pDestination);
    std::vector<uint64_t> GetDownloadList();
    std::tuple<std::wstring,double,double,double> GetDownloadInfo(uint64_t pID);

public:
    template <class tIntrospector>
    static void Introspect(tIntrospector &pI)
    {
        pI.template AddMethod<void(const io::CURI&,const io::CURI&)>("OpenTorrent",&CBitDaemon::OpenTorrent);
        pI.template AddMethod<std::vector<uint64_t>()>("GetDownloadList",&CBitDaemon::GetDownloadList);
        pI.template AddMethod<std::tuple<std::wstring,double,double,double>(uint64_t)>("GetDownloadInfo",
                                                                                       &CBitDaemon::GetDownloadInfo);
    }

private:
    std::unique_ptr<CTorrentManager> mTorrentManager;
    std::map<uint64_t,CDownload*> mDownloads;
};

extern CBitDaemon *gBitDaemon;

/*namespace bitdaemon*/ } /*namespace buola*/ }

#endif
